define(['bootstrap', 'backend', 'table', 'form'], function (undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            Table.api.init({
                extend: {
                    index_url: 'scanwork/schedule_task/index' + location.search,
                    add_url: 'scanwork/schedule_task/add',
                    edit_url: 'scanwork/schedule_task/edit',
                    del_url: 'scanwork/schedule_task/del',
                    multi_url: 'scanwork/schedule_task/multi',
                    table: 'scanwork_schedule_task',
                }
            });

            var table = $("#table");

            table.bootstrapTable({
                url: 'scanwork/schedule_task/index' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID', width: 50},
                        {field: 'task_no', title: '任务编号', width: 120},
                        {field: 'schedule_name', title: '排程名称', width: 120},
                        {field: 'process_name', title: '工序名称', width: 120},
                        {field: 'work_center_name', title: '工作中心', width: 100},
                        {field: 'equipment_name', title: '设备', width: 100},
                        {field: 'employee_name', title: '负责人', width: 80},
                        {field: 'quantity', title: '数量', width: 80, formatter: function(v) { 
                            return parseFloat(v || 0).toFixed(2); 
                        }},
                        {field: 'standard_time', title: '标准时间', width: 80, formatter: function(v) { 
                            return parseFloat(v || 0).toFixed(2) + 'h'; 
                        }},
                        {field: 'priority', title: '优先级', width: 80, formatter: function(v) {
                            var priorityMap = {10: '紧急', 30: '高', 50: '中', 70: '低', 90: '最低'};
                            var colorMap = {10: 'danger', 30: 'warning', 50: 'info', 70: 'success', 90: 'default'};
                            return '<span class="label label-' + (colorMap[v] || 'default') + '">' + (priorityMap[v] || '未知') + '</span>';
                        }},
                        {field: 'plan_start_time', title: '计划开始', width: 120, formatter: Table.api.formatter.datetime},
                        {field: 'plan_end_time', title: '计划结束', width: 120, formatter: Table.api.formatter.datetime},
                        {field: 'actual_start_time', title: '实际开始', width: 120, formatter: Table.api.formatter.datetime},
                        {field: 'actual_end_time', title: '实际结束', width: 120, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: '创建时间', width: 120, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
